home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / spectrogra < prev    next >
Text File  |  1995-02-25  |  2KB  |  74 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. // Synopsis:   Compute the short-time periodic windowed FFT of a signal.
  4.  
  5. // Syntax:     spectrogram ( SEQ, FRAMESIZE, NOVERLAP )
  6. //             spectrogram ( SEQ, FRAMESIZE, NOVERLAP, ZEROPAD )
  7.  
  8. // Description:
  9.  
  10. //      For each frame, window it with a Hanning window, compute the
  11. //      FFT, remove the negative frequencies, and take the log of
  12. //      the resulting elements.  Then hop ahead by (FRAMESIZE -
  13. //      NOVERLAP) elements to the next frame. 
  14.  
  15. //      If the ZEROPAD argument is supplied, that many zeros are
  16. //      appended to the frame before computing the FFT.
  17. //      ZEROPAD + FRAMESIZE should be a power of two.
  18.  
  19. //      The resulting array has (FRAMESIZE+ZEROPAD)/2 rows and a
  20. //      number of columns determined by the framesize and overlap
  21. //      values. Low frequencies come first, i.e. they have low row
  22. //      indices. 
  23.  
  24. // Sample seq:     seq = readb("somedatafile.rb");
  25. //                 spect = spectrogram(seq, 512, 0);
  26. //                 plmesh(<< x=1:spect.nr; y=1:spect.nc; z=spect >>);
  27.  
  28. // This file is a translation of spectrogram.m from the Osprey toolbox.
  29. // The results of this function may differ from Matlab's slightly 
  30. // since the Hanning window is computed differently.
  31.  
  32. require window
  33.  
  34. //-------------------------------------------------------------------//
  35.  
  36. spectrogram = function (seq, frameSize, nOverlap, zeroPad)
  37. {
  38.   local (seq, frameSize, nOverlap, zeroPad)
  39.  
  40.   if (!exist (zeroPad)) { zeroPad = 0; }
  41.  
  42.   inrows = seq.nr;
  43.   incols = seq.nc;
  44.  
  45.   if (inrows != 1 && incols != 1)
  46.   {
  47.     error ("Spectrogram requires a 1-dimensional sequence");
  48.   }
  49.  
  50.   nfft    = frameSize + zeroPad;
  51.   seqsize = max (inrows, incols);
  52.   outcols = floor(1 + (seqsize - frameSize) / (frameSize - nOverlap));
  53.   outrows = nfft / 2;
  54.   result  = zeros (outrows, outcols);
  55.  
  56.   // column vector
  57.   win  = window (frameSize, "hann");
  58.  
  59.   for (i in 1:outcols)
  60.   {
  61.     start = 1 + (frameSize - nOverlap) * (i - 1);
  62.     frame = reshape (seq[start:(start + frameSize - 1)], frameSize, 1);
  63.  
  64.     // column vector
  65.     spectrum = fft (frame .* win, nfft); 
  66.     result[;i] = spectrum [1:outrows];
  67.   }
  68.  
  69.   // move inside loop if memory problems
  70.   result = log (abs (result));
  71.  
  72.   return result;
  73. };
  74.